﻿using System;
using System.Data;
using System.Text;
using Devart.Data.Universal;
using OnOne.Lib;

namespace OnOne.Data
{
  public class ADO
  {
    private UniConnection conection = new Devart.Data.Universal.UniConnection();
    private Functions f = new Functions();
    public TConection _tconnection;
    #region Connection

    public UniConnection Conection()
    {
      return conection;
    }

    public bool DBConect()
    {
      if (conection.State == ConnectionState.Closed)
      {

        _tconnection.SetConection(ref conection);
        try
        {
          conection.Open();
          return true;
        }
        catch (Exception ex)
        {
          f.LogErro( ex.ToString() );
          return false;
        }
      }
      else
        return true;
    }

    // ----------> Disconecta do Servidor caso esteja conectado - 1/8/2012 10:25:11 <----------
    public void DBDisconect()
    {
      if (conection.State == ConnectionState.Open)
        conection.Close();
    }

    // ----------> Disconecta (Caso esteja conectado) e faz uma nova conexao - 1/8/2012 10:25:33 <----------
    public void DBReconect()
    {
      DBDisconect();
      DBConect();
    }
#endregion

#region Data
    // ----------> Executa um procedimento armazenado (Stored Procedure) - 1/8/2012 10:26:00 <----------
    public void DBExecProc(String sSP, String[] aParams, String[] aValues)
    {
      UniCommand cmd = new UniCommand(sSP, conection);
      int x;
      if (DBConect())
      {
        // seta os parametros e valores
        for (x = 0; x < aParams.Length; x++)
        {
          cmd.Parameters.Add(aParams[x], aValues[x]);
        }

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.ExecuteNonQuery();
      }
    }

    
  #region Executa Comandos SQL
    public TReturn DBExecSQL(string sSQL, Boolean CloseOpenCon = false, Boolean bShowMsgErro = false,
                             Boolean bThread = false)
    {
      TReturn rt = new TReturn();
      rt.bReturn = false;
      if (CloseOpenCon == true) DBDisconect();
      DBConect();
      try
      {
        UniCommand cmd = new UniCommand(sSQL, conection);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        
        rt.bReturn = true;
        return rt;
      }
      catch (Exception e)
      {
        rt.bReturn = false;
        rt.sMensagem = e.Message;
        f.LogErro(e.Message);
        return rt;
      }
    }

    public TReturn DBExecSQL(StringBuilder[] sb, Boolean CloseOpenCon = false, Boolean bShowMsgErro = false,
                             Boolean bThread = false)
    {
      TReturn rt = new TReturn();
      for (int x = 0; x < sb.Length; x++)
      {
        if (sb[x].Length > 0)
        {
          if (x == 0)
            rt = DBExecSQL(sb[x].ToString(), CloseOpenCon, bShowMsgErro, bThread);
          else
            DBExecSQL(sb[x].ToString(), CloseOpenCon, bShowMsgErro, bThread);
        }
      }
      return rt;
    }

  #endregion

    // ----------> Faz uma checagem em um campo de um DR ( Data Read - Dados para Leitura :-) ) se o campo esta nullo - 1/8/2012 10:27:49 <----------
    public byte[] DBHaveImages(UniDataReader dr, int iFldPos)
    {
      if (dr.IsDBNull(iFldPos) == true)
        return null;
      else
        return (byte[])dr.GetValue(iFldPos);
    }

    // ----------> Move Registros no DR - 1/8/2012 10:28:45 <----------
    public void DBRcMove(UniDataReader dr, EnumDRMove enumDrMove, int iPos = 0)
    {
      switch (enumDrMove)
      {
        case EnumDRMove.mvFirst:
          
          break;
        case EnumDRMove.mvPrior:
          break;
        case EnumDRMove.mvNext:
          break;
        case EnumDRMove.mvLast:
          break;
        case EnumDRMove.mvPosition:
          break;
      }
    }


    public Boolean DBRecExists(String sSQL)
    {
      DataSet ds = DBRequery(sSQL);
      bool bflag = (ds.Tables[0].Rows.Count > 0);
      ds.Dispose();
      return bflag;
    }

    public Boolean DBRecExists(UniDataReader dr)
    {
      Int16 x = 0;
      while (dr.Read())
      {
        x++;
      }
      dr.Dispose();
      return (x > 0);
    }
     

   public UniDataAdapter daDBRequery(string ssql, String sParamWhere = "")
   {
     UniDataAdapter da = new UniDataAdapter(ssql, conection);
     return da;
   }

    public DataSet DBRequery(string ssql, String sParamWhere = "")
    {
      DataSet ds = new DataSet();
      daDBRequery(ssql, sParamWhere).Fill(ds);
      return ds;
    }


    public Boolean DBSqlCommand(String sSQL, String[] aParmns, Object[] aValues)
    {
      DBReconect();

      UniCommand cmd = new UniCommand(sSQL);
      cmd.Connection = conection;
      for (int x = 0; x < aParmns.Length; x++)
        cmd.Parameters.Add(aParmns[x], aValues[x]);

      cmd.ExecuteNonQuery();
      conection.Close();

      return false;
    }
  }
#endregion

  public static class Dialogs
  {
    private static fLogin _fLogin = new fLogin();

    public static void FConect()
    {
      throw new System.NotImplementedException();
    }

    public static bool FLogin()
    {
      _fLogin.ShowDialog();
      return _fLogin.bflag;
    }
  }
}